﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class CalculateSlope
	{
		public CalculateSlope(double elevation, int iStartorEnd)
		{
			this.elevation = elevation;
			this.iStartorEnd = iStartorEnd;
		}

		public void MovePipe(UIDocument uiDoc)
		{
			try
			{
				Document document = uiDoc.Document;
				Reference reference = uiDoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new PipeSelectionFilter(), "选取一个管道");
				Element element = document.GetElement(reference);
				LocationCurve locationCurve = (element as Pipe).Location as LocationCurve;
				this.line = (locationCurve.Curve as Line);
				this.ptStart = this.line.GetEndPoint(0);
				this.ptEnd = this.line.GetEndPoint(1);
				if (this.iStartorEnd == 0)
				{
					XYZ point = new XYZ(this.ptStart.X, this.ptStart.Y, this.elevation / 304.8);
					Line curve = document.CreatYJKLine(point, this.ptEnd, true);
					locationCurve.Curve = curve;
				}
				else
				{
					XYZ endOrDirection = new XYZ(this.ptEnd.X, this.ptEnd.Y, this.elevation / 304.8);
					Line curve2 = document.CreatYJKLine(this.ptStart, endOrDirection, true);
					locationCurve.Curve = curve2;
				}
				ElementTransformUtils.MoveElement(document, element.Id, new XYZ(1E-05, 1E-05, 1E-05));
				ElementTransformUtils.MoveElement(document, element.Id, -1.0 * new XYZ(1E-05, 1E-05, 1E-05));
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
		}

		private double elevation;

		private int iStartorEnd;

		private Line line;

		private XYZ ptStart;

		private XYZ ptEnd;
	}
}
